package com.abei.gas.service.Impl;

import com.abei.gas.entity.Card;
import com.abei.gas.mapper.CardMapper;
import com.abei.gas.service.CardService;
import com.abei.gas.vo.DataVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CardServiceImpl implements CardService {
    @Resource
    private CardMapper cardMapper;
    /*
    * 查询所有
    * */
    @Override
    public DataVo<Card> findAll(Integer page,Integer limit) {
        List<Card> c=cardMapper.selectList(null);
        DataVo<Card> datavo=new DataVo<>();
        datavo.setCode(0);
        datavo.setMsg("");
        IPage<Card> cardpage=new Page<>(page,limit);
        IPage<Card> result=cardMapper.selectPage(cardpage,null);
        datavo.setCount(result.getTotal());
        c=result.getRecords();
        datavo.setData(c);
        return datavo;
    }

    /*
    * 根据id删除
    * */
    @Override
    public void deleteById(String cardid) {
        Map<String,Object> columMap=new HashMap<>();
        columMap.put("id",cardid);
        int i=cardMapper.deleteByMap(columMap);
        System.out.println(i);
    }

    /*
    * 添加
    * */
    @Override
    public void Insert(Card card) {
        Card u=new Card();
        u.setId(card.getId());
        u.setRest(card.getRest());
        u.setBalance(card.getBalance());
        u.setStatus(card.getStatus());
        u.setUserid(card.getUserid());
        cardMapper.insert(u);
    }

    /*
    * 根据id查询
    * */
    @Override
    public DataVo<Card> findById(String number) {
        QueryWrapper<Card> u = new QueryWrapper<>();
        u.like("id",number);
        List<Card> cardList=cardMapper.selectList(u);
        DataVo<Card> dataVO=new DataVo<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        Integer num = cardMapper.selectCount(null);
        dataVO.setCount(Long.valueOf(num));
        dataVO.setData(cardList);
        return dataVO;
    }

    /*
    * 更新
    * */
    @Override
    public void Update(String number, Card card) {
        QueryWrapper<Card> u= new QueryWrapper<>();
        u.eq("id",number);
        cardMapper.update(card,u);
    }

    @Override
    public void DeleteBatchIds(String ids) {
        String[] sArray=ids.split("&");
        List<String> id= new ArrayList<>();
        for (String i:sArray
        ) {
            id.add(i.substring(4));
        }
        int i = cardMapper.deleteBatchIds(id);
        System.out.println("影响的行数：" + i);
    }
}
